Adding the Sides of the "Triangle" Of the Ternary Plot¶

We let the result of the previous notebook rest and focus this notebook on implementing the sides of the triangle. As mentioned in the previous notebook we will need to use the Graph Objects API, which is the low-level API of Plotly. This API is more verbose than the Figure Factory API, but it is also more flexible. For documentation on the Graph Objects API see https://plotly.com/python-api-reference/plotly.graph_objects.html.

I have used the discussion at https://community.plotly.com/t/shapes-in-ternary-plot/38566/10 as a starting point for this notebook. The discussion does not contain a complete solution, but it does contain some useful hints. It shows how to draw lines in a ternary plot. By using the same approach we can draw the sides of the triangle. We just need to find the right coordinates for the lines. We also need to set the right mode and use the Scatterternary type, which is also used in the discussion.

In [ ]:
import plotly.graph_objects as go

sides = go.Scatterternary(
    a=[1, 0, 0, 1],
    b=[0, 1, 0, 0],
    c=[0, 0, 1, 0],
    mode="lines",
    line_width=4,
    line_color="black",
)

fig = go.Figure(data=[sides])
fig.show()

Set the Color of the Lines to Black¶

In [ ]:
import plotly.graph_objects as go

sides = go.Scatterternary(
    a=[1, 0, 0, 1],
    b=[0, 1, 0, 0],
    c=[0, 0, 1, 0],
    mode="lines",
    line_color="black", # This is a keyword argument
)

fig = go.Figure(data=[sides])
fig.show()

Remove the Grid of All the Axes¶

Apparently the grid is part of the figure. Or not? I am not sure. Maybe it's part of the layout? The surface area of the Plotly API is huge and I am not sure where to look for the documentation. The following code removes the grid of all the axes.

In [ ]:
import plotly.graph_objects as go

sides = go.Scatterternary(
    a=[1, 0, 0, 1],
    b=[0, 1, 0, 0],
    c=[0, 0, 1, 0],
    mode="lines",
    line_color="black", # This is a keyword argument
)

fig = go.Figure(data=[sides])
fig.update_ternaries(aaxis_showgrid=False)
fig.update_ternaries(baxis_showgrid=False)
fig.update_ternaries(caxis_showgrid=False)
fig.show()

The following code also removes the grid of the axes, but this time we specify it as a parameter when we create the figure. We create a dictionary with the layout and specify the showgrid parameter. We then pass this dictionary to the figure function.

In [ ]:
import plotly.graph_objects as go

sides = go.Scatterternary(
    a=[1, 0, 0, 1],
    b=[0, 1, 0, 0],
    c=[0, 0, 1, 0],
    mode="lines",
    line_color="black",  # This is a keyword argument
)

fig = go.Figure(
    data=[sides],
    layout={
        "ternary": {
            "aaxis": {"showgrid": False},
            "baxis": {"showgrid": False},
            "caxis": {"showgrid": False},
        }
    },
)
fig.show()

Remove the Color¶

In [ ]:
import plotly.graph_objects as go

sides = go.Scatterternary(
    a=[1, 0, 0, 1],
    b=[0, 1, 0, 0],
    c=[0, 0, 1, 0],
    mode="lines",
    line_color="black",  # This is a keyword argument
)

fig = go.Figure(
    data=[sides],
    layout={
        "ternary": {
            "aaxis": {"showgrid": False},
            "baxis": {"showgrid": False},
            "caxis": {"showgrid": False},
            "bgcolor": "rgba(0,0,0,0)",
        },
        "paper_bgcolor": "rgba(0,0,0,0)",
    },
)
fig.show()
In [ ]: